﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;

namespace infoMngmntGUI.Controller
{
    public class AccessManagement
    {
        #region Properties

        public delegate void userNameChanged(UserType type);
        public event userNameChanged UserLoggedIn;

        public enum UserType { NOTLOGGEDIN = 0, NURSE = 1, DOCTOR = 2, ADMIN = 3 };

        public UserType LoggedInUserType { get; private set;  }
        public string UserName { get; private set; }

        #endregion

        #region Construction

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string userName, string password)
        {
            string resultingUserName = DbConnection.Login(userName, password);
            if (resultingUserName != null)
            {
                LoggedInUserType = DbConnection.GetPermissionType(resultingUserName);
                if (UserLoggedIn != null)
                {
                    UserLoggedIn(LoggedInUserType);
                }
                return true;
            }
            return false;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        public void LogOut()
        {
            LoggedInUserType = UserType.NOTLOGGEDIN;
            UserName = null;

            if (UserLoggedIn != null)
            {
                UserLoggedIn(LoggedInUserType);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginTypeRequired"></param>
        /// <returns></returns>
        public bool CanAccess(UserType loginTypeRequired)
        {
            if(loginTypeRequired <= LoggedInUserType)
            {
                return true;
            }
            return false;
        }

        #endregion
    }
}
